 package cn.zclong.reply.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import cn.itcast.servlet.BaseServlet;
import cn.zclong.article.domain.PageBean;
import cn.zclong.reply.domain.Reply;
import cn.zclong.reply.service.ReplyService;

/**
 * 帖子
 */
public class ReplyServlet extends BaseServlet {
	private ReplyService replyService = new ReplyService();
	
	
	/*
	 * 发表评论
	 */
	public void addReply(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		
		try {
			String aid = request.getParameter("aid");//获取该帖子的id
			String message = request.getParameter("message");//获取评论的内容
			String userId = request.getParameter("userid");//获取评论者的id
			String ip = request.getRemoteAddr();//获取访问者的ip
			System.out.println("oooooooooo");
			if(userId.trim().isEmpty()){
				response.getWriter().print("0");
				return ;
			}else if(aid ==null || message ==null){
				response.getWriter().print("-1");
				return ;
			}
			replyService.addReply(aid, userId, message, ip);
			//直接返回1
			response.getWriter().print("1");	
		} catch (Exception e) {
			//出异常了，直接返回-1
			response.getWriter().print("-1");
		}
		
	}
		
	/*
	 * 显示帖子所有的回复内容，分页显示，根据帖子的id
	 */
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
		//先获取该帖子的id
		String id = request.getParameter("id");
		/*
		 * 分页查询所有数据
		 * 1，获取页面传递的pc当前页码
		 * 2，给定ps的值，每页记录数
		 * 3，使用pc和ps调用的service方法，得到PageBean,保存到request域
		 * 4，转发到in.jsp
		 */
		int pc = getPc(request);//得到pc
		int ps = 4;//给定ps的值，每页四行记录
		PageBean<Reply> pb = replyService.findAll(pc, ps, id);//传递pc和ps给service方法，得到PageBean
		
		//得到url，保存到pb中
		pb.setUrl(getUrl(request));
		
		 
		
//		//得到时间
//		
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		String time1 =  sdf.format(new Date(time));
//		request.setAttribute("time1", time1);
		
		request.setAttribute("pb", pb);//保存到request域
//		return "f:/jsps/article/tail.jsp";
	}
	
	/*
	 * 获取pc当前页码
	 */
	public int getPc(HttpServletRequest request) {
		String value = request.getParameter("pc");
		if(value == null || value.trim().isEmpty()) {
			return 1;//pc位null，表示只有一页
		}
		return Integer.parseInt(value);//把pc转换成int类型
	}

	/*
	 * 截取url
	 * 	/项目名/Servlet路径？参数字符串
	 */
	private String getUrl(HttpServletRequest request) {
		String contextPath = request.getContextPath();//获取项目名
		String servletPath = request.getServletPath();//获取servletpath，即/ReplyServlet
		String queryString = request.getQueryString();//获取问号之后的参数部分
		
		//判断参数部分中是否包含pc这个参数，如果包含，截取下来，不需要这一部分
		if(queryString.contains("&pc=")) {
			int index = queryString.lastIndexOf("&pc=");
			queryString = queryString.substring(0, index);
		}
		System.out.println(contextPath + servletPath + "?" + queryString);
		return  contextPath + servletPath + "?" + queryString;
		
	}
}
